package com.example.onlinemusicserver.controller;

import com.example.onlinemusicserver.mapper.UserMapper;
import com.example.onlinemusicserver.model.User;
import com.example.onlinemusicserver.service.UserService;
import com.example.onlinemusicserver.tools.Constant;
import com.example.onlinemusicserver.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

   // BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();

    @RequestMapping("/testEx")
    public int test(){
        int n=10/0;
        return n;
    }

    /*
    未加密登录
     */
    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(@RequestParam String username, @RequestParam String password,
                                           HttpServletRequest request) {
        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);

        User user = userService.login(userLogin);
        if (user != null) {
            System.out.println("登录成功");
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY, user);
            return new ResponseBodyMessage<>(0, "登录成功", userLogin);
        } else {
            System.out.println("登录失败");
            return new ResponseBodyMessage<>(-1, "登录失败", userLogin);
        }
    }

    /*
    加密登录
     */
    @RequestMapping("/login1")
    public ResponseBodyMessage<User> login1(@RequestParam String username, @RequestParam String password,
                                            HttpServletRequest request) {
        User user = userService.login1(username);
        if (user == null) {
            System.out.println("登录失败");
            return new ResponseBodyMessage<>(-1, "登录失败，用户名或密码错误", user);
        } else {
            boolean flg = bCryptPasswordEncoder.matches(password, user.getPassword());
            if (!flg) {
                return new ResponseBodyMessage<>(-1, "登录失败，用户名或密码错误", user);
            }
            request.getSession().setAttribute(Constant.USERINFO_SESSION_KEY, user);
            System.out.println("登录成功");
            return new ResponseBodyMessage<>(0,"登录成功!",user);
        }

    }


    // 注册

    @RequestMapping("/register")
    public ResponseBodyMessage<Boolean> registerUser(@RequestParam String username,@RequestParam String password){
        User user=userMapper.selectByName(username);
        if (user!=null){
            return new ResponseBodyMessage<>(-1,"注册用户失败，当前已存在此用户名",false);
        }
        String passwordMD=bCryptPasswordEncoder.encode(password);
        System.out.println(passwordMD);
        int ret=userMapper.register(username,passwordMD);
        if (ret==1){
            return new ResponseBodyMessage<>(0,"注册用户成功",true);
        }else {
            return new ResponseBodyMessage<>(-1,"注册用户失败",false);
        }
    }


    // 注销
    @RequestMapping("/logout")
    public ResponseBodyMessage<Boolean> logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session=request.getSession(false);
        System.out.println(session);
        if (session==null){
            System.out.println("当前用户未登录，无法注销");
            return new ResponseBodyMessage<>(0,"当前用户未登录，无法注销",false);
        }
        session.removeAttribute(Constant.USERINFO_SESSION_KEY);
        System.out.println("注销成功！");
        return new ResponseBodyMessage<>(0,"注销成功",true);
    }


}
